package cn.topkinson.rabbitmq.dead.queue.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author huangzihao
 * @Date 2020/12/26 19:39
 */
@Configuration
public class RabbitConfig {

    public static final String Q_PAY = "q.pay";
    public static final String EX_PAY = "ex.pay";
    public static final String KEY_PAY = "pay";
    public static final String Q_PAY_DLX = "q.pay.dlx";
    public static final String EX_PAY_DLX = "ex.pay.dlx";
    public static final String KEY_PAY_DLX = "pay.dlx";

    /** ==================支付死信：队列、交换机、绑定 ================== */
    @Bean
    public Queue payQueueDlx() {
        return new Queue(Q_PAY_DLX, true, false, false);
    }
    @Bean
    public Exchange payExchangeDlx() {
        return new DirectExchange(EX_PAY_DLX, true, false, null);
    }
    @Bean
    public Binding bindingDlx() {
        return BindingBuilder.bind(payQueueDlx()).to(payExchangeDlx()).with(KEY_PAY_DLX).noargs();
    }


    /** ==================支付：队列、交换机、绑定 ================== */
    @Bean
    public Queue payQueue() {
        Map<String, Object> props = new HashMap<>();
        // 消息的生存时间10s
        props.put("x-message-ttl", 10000);
        //设置该队列所关联的死信交换器（当队列消息TTL到期后依然没有消费，则加 入死信队列）
        props.put("x-dead-letter-exchange", EX_PAY_DLX);
        // 设置该队列所关联的死信交换器的routingKey，如果没有特殊指定，使用原 队列的routingKey
        props.put("x-dead-letter-routing-key", KEY_PAY_DLX);
        Queue queue = new Queue(Q_PAY, true, false, false, props);
        return queue;
    }
    @Bean
    public Exchange payExchange() {
        return new DirectExchange(EX_PAY, true, false, null);
    }
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(payQueue()).to(payExchange()).with(KEY_PAY).noargs();
    }


}
